package com.atguigu.tingshu.album.receiver;

import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.vo.album.TrackStatMqVo;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author dxt
 * @create 2025-06-25-19:09
 */
@Component
@Slf4j
public class AlbumReceiver {

    @Autowired
    private TrackInfoService trackInfoService;

    @Autowired
    private AlbumInfoService albumInfoService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_TRACK_STAT_UPDATE, durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_TRACK),
            key = {MqConst.ROUTING_TRACK_STAT_UPDATE}
    ))
    public void updateTrackAndAlbumStat(String msg, Message message, Channel channel) {
        TrackStatMqVo trackStatMqVo = com.alibaba.fastjson.JSON.parseObject(msg, TrackStatMqVo.class);
        Long trackId = trackStatMqVo.getTrackId();
        Long albumId = trackStatMqVo.getAlbumId();
        String statType = trackStatMqVo.getStatType();
        Integer count = trackStatMqVo.getCount();
        String businessNo = trackStatMqVo.getBusinessNo();
        //幂等性校验
        String key = "Mq1:" + businessNo;

        try {
            Boolean ifAbsent = redisTemplate.opsForValue().setIfAbsent(key, businessNo, 5, TimeUnit.MINUTES);
            if (ifAbsent){
                //更新声音统计
                trackInfoService.updateTrackStat(trackId, statType, count);

                if (SystemConstant.TRACK_STAT_PLAY.equals(statType)){
                    //更新专辑统计
                    albumInfoService.updateAlbumStat(albumId, SystemConstant.ALBUM_STAT_PLAY, count);
                }else if (SystemConstant.TRACK_STAT_COMMENT.equals(statType)){
                    //更新专辑统计
                    albumInfoService.updateAlbumStat(albumId, SystemConstant.ALBUM_STAT_COMMENT, count);
                }
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            redisTemplate.delete(key);
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }
    }
}
